using System.Collections.Generic;

namespace Framework
{
    public static class DictionaryExtension
    {
        public static void Set<T>(this IDictionary<string, T> dictionary, string key, T value)
        {
            if (dictionary.ContainsKey(key))
            {
                dictionary[key] = value;
            }
            else
            {
                dictionary.Add(key, value);
            }
        }

        // *********************************************************************************
        // Dictionary AsValue Extensions
        // *********************************************************************************

        /// <summary>
        /// Lookup Key/Value as bool.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
        /// <returns></returns>
        public static bool LookupAsBool(this IDictionary<string, string> dictionary, string key, bool defaultValue = false)
        {
            return dictionary.ContainsKey(key) ? dictionary[key].AsBool(defaultValue) : defaultValue;
        }

        /// <summary>
        /// Lookup Key/Value as int.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
        /// <returns></returns>
        public static int LookupAsInt(this IDictionary<string, string> dictionary, string key, int defaultValue = -1)
        {
            return dictionary.ContainsKey(key) ? dictionary[key].AsInt(defaultValue) : defaultValue;
        }
    }
}